package ai.zixing.linkedlist;

public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        System.out.println("双向链表测试");
        // 创建节点
        HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
//        doubleLinkedList.add(heroNode1);
//        doubleLinkedList.add(heroNode3);
//        doubleLinkedList.add(heroNode2);
//        doubleLinkedList.add(heroNode4);
        doubleLinkedList.addByOrder(heroNode3);
        doubleLinkedList.addByOrder(heroNode1);
        doubleLinkedList.addByOrder(heroNode4);
        doubleLinkedList.addByOrder(heroNode2);
        doubleLinkedList.list();
//        System.out.println("双向链表修改测试");
//        HeroNode2 newHeroNode = new HeroNode2(4, "公孙胜", "入云龙");
//        doubleLinkedList.update(newHeroNode);
//        System.out.println("========修改后链表========");
//        doubleLinkedList.list();
//        System.out.println("========删除测试========");
//        doubleLinkedList.del(3);
//        System.out.println("========删除后链表========");
//        doubleLinkedList.list();
    }
}

class DoubleLinkedList {
    private HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead() {
        return head;
    }

    public void list() {
        if (head.next == null) {
            System.out.println("该双向链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public void add(HeroNode2 heroNode2) {
        HeroNode2 temp = head;
        // 遍历找到最后一个节点 temp
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        // 形成一个双向链表
        temp.next = heroNode2;
        heroNode2.pre = temp;
    }

    public void addByOrder(HeroNode2 heroNode2) {
        // 需要新增的前一个节点信息
        HeroNode2 preNode = head;
        // 需要新增的后一个节点信息
        HeroNode2 nexNode = null;
        boolean flag = false;
        while (true) {
            // 链表遍历完，跳出
            if (preNode.next == null) {
                break;
            }
            if (preNode.next.no > heroNode2.no) {
                nexNode = preNode.next;
                break;
            }
            if (preNode.next.no == heroNode2.no) {
                flag = true;
                break;
            }
            preNode = preNode.next;
        }
        if (flag) {
            System.out.println("已存在");
        } else {
            // 在两个节点中添加一个节点，需要修改两个连接，增加两个连接
            // 修改插入节点的前一个节点的next
            preNode.next = heroNode2;
            // 新增插入节点的前驱节点为前一个节点
            heroNode2.pre = preNode;
            // 新增插入节点的后驱节点
            heroNode2.next = nexNode;
            // 旧后驱节点的pre 指向新增节点
            if (nexNode != null) {
                nexNode.pre = heroNode2;
            }
        }
    }

    public void update(HeroNode2 heroNode2) {
        if (head.next == null) {
            System.out.println("该双向链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == heroNode2.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.name = heroNode2.name;
            temp.nickName = heroNode2.nickName;
        } else {
            System.out.printf("没有找到编号为 %d 的节点\n", heroNode2.no);
        }
    }

    public void del(int no) {
        HeroNode2 temp = head.next;
        if (temp == null) {
            System.out.println("kong");
        }
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            // 如果是最后一个节点不需要执行
            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }
        } else {
            System.out.printf("要删除的 %d 的节点不存在", no);
        }
    }
}

// 定义 HeroNode，HeroNode
class HeroNode2 {
    public int no;
    public String name;
    public String nickName;
    // 指向下一个节点
    public HeroNode2 next;
    public HeroNode2 pre;

    HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}

